<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org">
<head>
    <head>
        <th:block th:include="include :: header('用户列表')"/>
    </head>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="searchForm">
                <div class="select-list">
                    <ul>
                        <li>
                            登录名：<input type="text" id="loginName"/>
                        </li>
                        <li>
                            用户名：<input type="text" id="userName"/>
                        </li>
                        <li>
                            手机号：<input type="text" id="phone"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" id="searchButton"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" id="resetButton"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="add();" data-role="system:user:add">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-primary single disabled" onclick="edit(this);" data-id="0" data-role="system:user:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="removeAll(this);" data-id="0" data-role="system:user:delete">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning single disabled" onclick="resetPwd(this);" data-id="0" data-role="system:user:password">
                <i class="fa fa-key"></i> 重置密码
            </a>
            <a class="btn btn-info" onclick="exportUser()" data-role="system:user:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="dataTable"></table>
        </div>
    </div>
</div>

<th:block th:include="include :: footer"/>
<!-- jquery-validate 表单树插件 -->
<script type="text/javascript" th:inline="javascript">
    let $table;
    $(function () {
        loadData();

        jutils.handleMessage(function(res){
            //console.log(res);
            if(res.title==='userForm'){
                $table.easyTable('search');
            }

        });

       // console.log('按钮权限',top.buttonPermissionModel);
    });


    function loadData(){
         let options={
             url: '/admin/sysuser/getUserList',
             pageNumber: 1,                      // 初始化加载第一页，默认第一页,并记录
             pageSize: 10,                       // 每页的记录行数（*）
             pageList: "10, 25, 50, 100",        // 可供选择的每页的行数（*）
             queryParams: function (params) {
                 let p = {
                     pageNumber: params.pageNumber,
                     pageSize: params.pageSize,
                     loginName:$('#loginName').val(),
                     userName:$('#userName').val(),
                     phone:$('#phone').val()
                 }
                 return p;
             },
             columns: [
                 {field: 'selectItem', checkbox: true},
                 {
                     field: 'loginName', title: '登录账号', align: "center"
                 },
                 {field: 'userName', title: '用户名', align: "center"},
                 {field: 'mobilePhone', title: '联系电话', align: "center"},
                 {field: 'email', title: '邮箱', align: "center"},
                 {field: 'deptName', title: '部门', align: "center"},
                 {field: 'isSuper',title: '超管',align: 'center',formatter:function(value,row,index){
                              if(value===1){
                                  return '<span class="badge badge-primary">是</span>'
                              }
                     }},
                 {field: 'roleName', title: '归属角色', align: "center"},
                 {field: 'userStatus',title: '用户状态',align: 'center',formatter:function(value,row,index){
                         if(value===1){
                             return '<span class="badge badge-primary">正常</span>';
                         }
                         return '<span class="badge danger">停用</span>';
                     }},
                 {field: 'createTime', title: '创建时间', align: "center"},
                 {
                     field: 'id',title: '操作', align: "center", formatter: renderOperationButton
                 }
             ]
         };

        $table=$('#dataTable').easyTable(options);
    }

    function  renderOperationButton(value,row,index){
        let actions = [];
        actions.push({
            roleTag:'system:user:edit',
            button:'<a class="btn btn-success btn-xs" href="javascript:void(0);" onclick="edit(this);" data-id="' + value + '"><i class="fa fa-edit"></i>编辑</a>'
        });
        actions.push({
            roleTag:'system:user:delete',
            button:'<a class="btn btn-danger btn-xs" href="javascript:void(0);" onclick="removeAll(this);" data-id="' + value + '"><i class="fa fa-trash"></i>删除</a>'
        });

        return jutils.buttonPermission(actions);
    }


    function add(){
        jutils.openTab('userTab','添加用户','/admin/sysuser/form?id='+0);
    }

    function edit(obj){
        let id=$(obj).data('id');
        if(id==0){
            const rows=$table.easyTable("selected");
            if(rows.length==0){
                jutils.warn("请选择数据行");
                return;
            }
            id=rows[0].id;
        }

        jutils.openTab('userTab','修改用户','/admin/sysuser/form?id='+id);
    }

    function removeAll(obj){
        const id=$(obj).data('id');
        let del=false;
        let ids=[];
        if(id==0){
            const rows=$table.easyTable("selected");
            if(rows.length==0){
                jutils.warn("请选择数据行");
                return;
            }
            for (let i=0;i<rows.length;i++){
                ids.push(rows[i].id);
            }
        }else{
            ids.push(id);
        }


        jutils.confirm("确认删除用户数据吗？",function(){
            jutils.ajaxGet("/admin/sysuser/removeAll",{ids:ids.join()},function(res){
                              if(res.status){
                                  $table.easyTable('search');
                              }
            });
        });
    }

      function resetPwd(){
          const rows=$table.easyTable("selected");
          if(rows.length==0){
              jutils.warn("请选择数据行");
              return;
          }

          jutils.dialog('重置密码','/admin/sysuser/resetPasswordView',{id:rows[0].id,loginName:rows[0].loginName},["600px","300px"],function(){

          });
      }

      function exportUser(){
          jutils.confirm("确认导出所有用户数据吗？",function(){
              location.href ="/admin/sysuser/ExportUser";
          });
      }
</script>
</body>
</html>